package hfuu.com.cn.teasystemapp;

import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.regex.Pattern;

import hfuu.com.cn.teasystemapp.bean.operategz;
import hfuu.com.cn.teasystemapp.bean.operatern;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class gzadd extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gzadd);

        Button ok= (Button) findViewById(R.id.ok);
        Button cancel= (Button) findViewById(R.id.cancel);
        Intent i=getIntent();
        final String id=i.getStringExtra("id");
        final String teaxid=i.getStringExtra("teaxid");

        final EditText timet= (EditText) findViewById(R.id.time);
        final EditText wd1t= (EditText) findViewById(R.id.wd1);
        final EditText wd2t= (EditText) findViewById(R.id.wd2);
        final EditText totalt= (EditText) findViewById(R.id.total);

        ImageView back = (ImageView) findViewById(R.id.back);



        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time=timet.getText().toString().trim();
                String wd1=wd1t.getText().toString().trim();
                String wd2=wd2t.getText().toString().trim();
                String total=totalt.getText().toString().trim();
                 if (time.length()==0&&wd1.length()==0&&wd2.length()==0&&total.length()==0){
                    Toast.makeText(gzadd.this,"请输入内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                Pattern pattern=Pattern.compile("^((0)|([1-9][0-9]*))\\.[0-9]+$");
                Pattern p = Pattern.compile("^[-\\+]?[\\d]*$");
                if(!(pattern.matcher(time).matches()||p.matcher(time ).matches()||pattern.matcher(wd1).matches()||p.matcher(wd1 ).matches()||pattern.matcher(wd2).matches()||p.matcher(wd2 ).matches()||pattern.matcher(total).matches()||p.matcher(total ).matches())){
                    Toast.makeText(gzadd.this,"输入数字",Toast.LENGTH_SHORT).show();
                    return;
                }
               operategz gz=new operategz();
                gz.setOperateQuality(Float.parseFloat(total));
                gz.setOperateWd1(wd1);
                gz.setOperateWd2(wd2);
                gz.setOperateTotaltime(time);
                gz.setOperateId(id);
                gz.setOperateTeaxid(teaxid);

                Gson gson=new Gson();
                String pickjson= gson.toJson(gz);

                HttpUtils.doPost("operate/gzprocad", pickjson, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Looper.prepare();
                        Toast.makeText(gzadd.this, "网络连接失败", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res=response.body().string();
                        if(res.equals("success")){
                            Looper.prepare();
                            Toast.makeText(gzadd.this, "添加成功", Toast.LENGTH_LONG).show();
                            Looper.loop();

                            gzadd.this.finish();
                            return;
                        }else{
                            Looper.prepare();
                            Toast.makeText(gzadd.this, "添加失败", Toast.LENGTH_LONG).show();
                            Looper.loop();
                        }
                    }
                });
                finish();
            }
        });

    }
}
